package cn.z2huo.demo.spring.webmvc.service;

import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReadConfig;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.z2huo.demo.spring.webmvc.entity.dto.No3CsvDataDTO;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * <p>
 *
 * @author z2huo
 */
@Service
public class ReadCsvFileServiceImpl {

    public List<No3CsvDataDTO> readNo3CsvFile(InputStream inputStream) {
        CsvReadConfig csvReadConfig = CsvReadConfig.defaultConfig();
        csvReadConfig.setHeaderLineNo(0).setBeginLineNo(0);

        List<No3CsvDataDTO> testData;

        try (InputStreamReader fileReader = new InputStreamReader(inputStream);
             CsvReader reader = CsvUtil.getReader(fileReader, csvReadConfig);) {

            CsvData csvData = reader.read();

            testData = csvData.getRows().parallelStream().map(row -> row.toBean(No3CsvDataDTO.class)).toList();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return testData;
    }

}
